package com.xian.im.modules.command.group;


import com.xian.im.common.exception.BusinessException;

import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: xian-im-server
 * @description:
 * @author: liru.xian
 * @create: 2021-01-27 09:26
 **/
public class GroupHandlerCommandFactory {

    private static Map<String, GroupCommand> map = new ConcurrentHashMap<>();
    static{
        ServiceLoader<GroupCommand> loader = ServiceLoader.load(GroupCommand.class);
        for (GroupCommand command : loader) {
            map.put(command.cmdCode(),command);
        }
    }

    public static GroupCommand type(String cmdCode) {
        GroupCommand command = map.get(cmdCode);
        if (command != null) {
            return command;
        }
        throw new BusinessException("参数错误");
    }
}
